﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ComponentModel;

namespace MyVideoViewerLibrary
{
    public enum MovieSource
    {
        MyMovies,
        MyMoviesXML,
        EAX
    }

    [XmlRoot("Settings")]
    public class Settings : INotifyPropertyChanged
    {
        const string CONFIG_FILE = @"MyVideoViewer.config";
        const string CONFIG_FOLDER_NAME = @"MyVideoViewer";
        public const string DATABASE_FILE = @"VideoViewer.sdf";
        public const string SHARE_NAME = "MyVideoViewer$";

        private static Settings singleSettingsInstance;

        public static string DataFolder
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), CONFIG_FOLDER_NAME);
            }
        }
        public static string ConfigFile
        {
            get
            {
                return Path.Combine(DataFolder, CONFIG_FILE);
            }
        }
        public string ShareLocation
        {
            get
            {
                return string.Format(@"\\{0}\{1}\", VideoViewerServerName, SHARE_NAME);
            }
        }
        public string DatabaseFile
        {
            get
            {
                return Path.Combine(ShareLocation, DATABASE_FILE);
            }
        }
        public string MyMoviesConnectionString
        {
            get
            {
                return string.Format(@"Data Source={0}\MYMOVIES;Initial Catalog='My Movies';Persist Security Info=True;User ID=sa;Password=t9AranuHA7", MyMoviesServerName);
            }
        }


        public static Settings Default
        {
            get
            {
                if (singleSettingsInstance == null)
                {
                    try
                    {
                        if (File.Exists(ConfigFile))
                        {
                            XmlSerializer s = new XmlSerializer(typeof(Settings));
                            TextReader r = new StreamReader(ConfigFile);
                            singleSettingsInstance = (Settings)s.Deserialize(r);
                            r.Close();                            
                        }
                        else
                            throw new Exception("Config file does not exist.");
                    }
                    catch
                    {
                        singleSettingsInstance = new Settings();
                    }


                }
                return singleSettingsInstance;
            }
        }

        private string trailersDownloadFolder;
        private string myMoviesServerName;
        private string videoViewerServerName;
        private bool downloadHD;
        private bool try1080;
        private bool downloadG;
        private bool downloadPG;
        private bool downloadPG13;
        private bool downloadR;
        private bool downloadNotYetRated;
        private string eaxMovieCatalogFile;
        private string myMoviesXmlFile;
        private MovieSource movieSource;

        [XmlElement("TrailersDownloadFolder")]
        public string TrailersDownloadFolder
        {
            get { return trailersDownloadFolder; }
            set
            {
                trailersDownloadFolder = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TrailersDownloadFolder"));
            }
        }

        [XmlElement("MyMoviesServerName")]
        public string MyMoviesServerName
        {
            get { return myMoviesServerName; }
            set
            {
                myMoviesServerName = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MyMoviesServerName"));
            }
        }

        [XmlElement("EaxMovieCatalogFile")]
        public string EaxMovieCatalogFile
        {
            get { return eaxMovieCatalogFile; }
            set
            {
                eaxMovieCatalogFile = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("EaxMovieCatalogFile"));
            }
        }

        [XmlElement("MyMoviesXmlFile")]
        public string MyMoviesXmlFile
        {
            get { return myMoviesXmlFile; }
            set
            {
                myMoviesXmlFile = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MyMoviesXmlFile"));
            }
        }

        [XmlElement("VideoViewerServerName")]
        public string VideoViewerServerName
        {
            get { return videoViewerServerName; }
            set
            {
                videoViewerServerName = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("VideoViewerServerName"));
            }
        }

        [XmlElement("DownloadHD")]
        public bool DownloadHD
        {
            get { return downloadHD; }
            set
            {
                downloadHD = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadHD"));
            }
        }

        [XmlElement("Try1080")]
        public bool Try1080
        {
            get { return try1080; }
            set
            {
                try1080 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Try1080"));
            }
        }

        [XmlElement("DownloadG")]
        public bool DownloadG
        {
            get { return downloadG; }
            set
            {
                downloadG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadG"));
            }
        }

        [XmlElement("DownloadPG")]
        public bool DownloadPG
        {
            get { return downloadPG; }
            set
            {
                downloadPG = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadPG"));
            }
        }

        [XmlElement("DownloadPG13")]
        public bool DownloadPG13
        {
            get { return downloadPG13; }
            set
            {
                downloadPG13 = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadPG13"));
            }
        }

        [XmlElement("DownloadR")]
        public bool DownloadR
        {
            get { return downloadR; }
            set
            {
                downloadR = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadR"));
            }
        }

        [XmlElement("DownloadNotYetRated")]
        public bool DownloadNotYetRated
        {
            get { return downloadNotYetRated; }
            set
            {
                downloadNotYetRated = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("DownloadNotYetRated"));
            }
        }

        [XmlElement("MovieSource")]
        public MovieSource MovieSource
        {
            get { return movieSource; }
            set
            {
                movieSource = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("MovieSource"));
            }
        }

        public void Save()
        {
            FileInfo file = new FileInfo(ConfigFile);
            if (!file.Directory.Exists)
                file.Directory.Create();

            XmlSerializer s = new XmlSerializer(typeof(Settings));
            TextWriter w = new StreamWriter(file.FullName);
            s.Serialize(w, Default);
            w.Close();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
